package freaks.terah.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import freaks.terah.model.Observer;
import freaks.terah.model.PlayerModel;

public class RessourceFrame extends JFrame  implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private PlayerModel theModel;
	private RessourceCountPanel currentRes;
	private RessourceCountPanel structUpkeep;
	private RessourceCountPanel production;
	private RessourceCountPanel unitUpkeep;
	private RessourceCountPanel netRes;
	private int[] net;

	/**
	 * Create the frame.
	 */
	public RessourceFrame(PlayerModel model) {
		setTitle("Ressource Management");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 330);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		currentRes = new RessourceCountPanel(0, true);
		currentRes.setBounds(151, 44, 330, 32);
		contentPane.add(currentRes);
		
		structUpkeep = new RessourceCountPanel(0, true);
		structUpkeep.setBounds(151, 99, 330, 32);
		contentPane.add(structUpkeep);
		
		unitUpkeep = new RessourceCountPanel(0, true);
		unitUpkeep.setBounds(151, 154, 330, 32);
		contentPane.add(unitUpkeep);
		
		production = new RessourceCountPanel(0, true);
		production.setBounds(151, 209, 330, 32);
		contentPane.add(production);
		
		JLabel label = new JLabel("-");
		label.setBounds(137, 79, 14, 16);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("-");
		label_1.setBounds(137, 135, 14, 16);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("=");
		label_2.setBounds(137, 244, 14, 16);
		contentPane.add(label_2);
		
		JLabel lblNewLabel = new JLabel("Current Ressources:");
		lblNewLabel.setBounds(6, 57, 133, 16);
		contentPane.add(lblNewLabel);
		
		JLabel lblStructureUpkeep = new JLabel("Structure Upkeep:");
		lblStructureUpkeep.setBounds(6, 110, 133, 16);
		contentPane.add(lblStructureUpkeep);
		
		JLabel lblUnitUpkeep = new JLabel("Unit Upkeep:");
		lblUnitUpkeep.setBounds(6, 166, 133, 16);
		contentPane.add(lblUnitUpkeep);
		
		netRes = new RessourceCountPanel(0, true);
		netRes.setBounds(151, 264, 330, 32);
		contentPane.add(netRes);
		
		JLabel label_3 = new JLabel("+");
		label_3.setBounds(137, 190, 14, 16);
		contentPane.add(label_3);
		
		JLabel lblProduction = new JLabel("Production:");
		lblProduction.setBounds(6, 220, 133, 16);
		contentPane.add(lblProduction);
		
		JLabel lblNetRessources = new JLabel("Net ressources:");
		lblNetRessources.setBounds(6, 272, 133, 16);
		contentPane.add(lblNetRessources);
		
		theModel = model;
		theModel.addListener(this);
		
		notifyMe();
	}
	
	public boolean isNetPositive()
	{
		for (int i = 0; i < 9; i++)
		{
			if (net[i] < 0)
				return false;
		}
		
		return true;
	}

	@Override
	public void notifyMe() {
		int[] curRes = theModel.getPlayerRessources();
		int[] sUp = theModel.getStructureUpkeep();
		int[] uUp = theModel.getUnitUpkeep();
		int[] pro = theModel.getProduction();
		net = new int[]{0,0,0,0,0,0,0,0,0};
		
		for (int i = 0; i < 9; i++)
		{
			net[i] = curRes[i] - sUp[i] - uUp[i] + pro[i];
			if (net[i] > 10 && i < 6)
				net[i] = 10;
		}	
		
		currentRes.setRessource(curRes, null);
		structUpkeep.setRessource(sUp, null);
		unitUpkeep.setRessource(uUp, null);
		production.setRessource(pro, null);
		netRes.setRessource(net, null, curRes);
	}
}
